package com.example.whereismystuff.views;

import java.util.ArrayList;
import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.Item;
import com.example.whereismystuff.classes.LoginDataBaseAdapter;
import com.example.whereismystuff.classes.User;
import com.example.whereismystuff.classes.UserDataManager;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * This method is in charge of getting all the users from the database and displaying them on the 
 * screen for use by the admin
 * @author NullPointers
 *
 */
public class UserView extends ListActivity {
	String[] from;
	Intent i;
	ArrayList<String> usernames;
	int count = 0;
	LoginDataBaseAdapter mySQLiteAdapter;
	String[] unames;
	@SuppressWarnings("deprecation")
	
	/**
	 * Method used to Instantiate Object and set the Layout for the Activity
	 * @param Bundle
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mySQLiteAdapter = new LoginDataBaseAdapter(this);
		mySQLiteAdapter.open();

		Cursor cursor = mySQLiteAdapter.queueAll();
		startManagingCursor(cursor);
		usernames = mySQLiteAdapter.queueAllUsers();
		unames = new String[usernames.size()];
		for(int i = 0; i < usernames.size(); i++){
			unames[i] = usernames.get(i).toString();
		}
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, unames));
		mySQLiteAdapter.close();
	}
	/**
	 * Creates the menu when the menu button is pressed
	 */
	public boolean onCreateOptionsMenu(Menu menu){
		new MenuInflater(this).inflate(R.menu.user_view_menu, menu);
		return(super.onCreateOptionsMenu(menu));
	}
	
	/**
	 * Redirects the user to the method based on which menu button was pressed
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		 switch (item.getItemId()) {
	        case R.id.u_logOut:
	            moveToUserLogin();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	/**
	 * Redirects the user to the admin edit layout when a username is clicked
	 */
	@Override
	protected void onListItemClick(ListView list, View view, int position, long id){
		super.onListItemClick(list, view, position, id);
		String un = unames[position];
		try
		{
			Toast.makeText(getApplicationContext(), "Clicked on :" + un, Toast.LENGTH_SHORT).show();
			Class c = Class.forName("com.example.whereismystuff.views.AdminEdit");
			Intent intent = new Intent(this, c);
			mySQLiteAdapter.open();
			String password = mySQLiteAdapter.getSingleEntry(un); 
			String locked = mySQLiteAdapter.getSingleEntryLock(un); 
			String admin = mySQLiteAdapter.getSingleEntryAdmin(un);
			mySQLiteAdapter.close();
			Bundle dataBundle = new Bundle();
			dataBundle.putString("Username", un);
			dataBundle.putString("Password", password);
			dataBundle.putString("Locked", locked);
			dataBundle.putString("Admin", admin);
			intent.putExtras(dataBundle);
			startActivity(intent);
			finish();
		}
		catch(ClassNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * Returns the admin to the login page
	 */
	public void moveToUserLogin(){
		Class c;
		try {
			c = Class.forName("com.example.whereismystuff.views.AdminLogin");
			Intent intent = new Intent(this, c);
			startActivity(intent);
			finish();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
